package com.example.david.stockpercents;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.tv_old_total)
    TextView tvOldTotal;
    @BindView(R.id.tv_old_get)
    TextView tvOldGet;
    @BindView(R.id.tv_old_percent)
    TextView tvOldPercent;
    @BindView(R.id.tv_old_num)
    EditText tvOldNum;
    @BindView(R.id.tv_old_amount)
    EditText tvOldAmount;
    @BindView(R.id.tv_old_cur_amount)
    EditText tvOldCurAmount;
    @BindView(R.id.tv_new_total)
    TextView tvNewTotal;
    @BindView(R.id.tv_new_get)
    TextView tvNewGet;
    @BindView(R.id.tv_new_percent)
    TextView tvNewPercent;
    @BindView(R.id.tv_new_num)
    TextView tvNewNum;
    @BindView(R.id.tv_new_amount)
    TextView tvNewAmount;
    @BindView(R.id.tv_new_cur_amount)
    TextView tvNewCurAmount;
    @BindView(R.id.et_newbuy_amount)
    EditText etNewbuyAmount;
    @BindView(R.id.et_newbuy_num)
    EditText etNewbuyNum;
    @BindView(R.id.btn_calculate)
    Button btnCalculate;

    BigDecimal bdNum, bdNewbyNum;
    BigDecimal bdOldAmount, bdOldCurAmount, bdNewbuyAmount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        btnCalculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculate();
            }
        });
    }

    private void initView() {
        String oldamount = tvOldAmount.getText().toString();
        String oldCurAmount = tvOldCurAmount.getText().toString();
        String oldNum = tvOldNum.getText().toString();

        String newbuyNum = etNewbuyNum.getText().toString();
        String newbuyAmount = etNewbuyAmount.getText().toString();


        if (TextUtils.isEmpty(oldamount)
                || TextUtils.isEmpty(oldCurAmount)
                || TextUtils.isEmpty(oldNum)
                || TextUtils.isEmpty(newbuyNum)
                || TextUtils.isEmpty(newbuyAmount)) {
            Toast.makeText(this, "输入内容", Toast.LENGTH_SHORT).show();
            return;
        }

        bdOldAmount = new BigDecimal(oldamount);
        bdOldCurAmount = new BigDecimal(oldCurAmount);

        bdNum = new BigDecimal(oldNum);
        bdNewbyNum = new BigDecimal(newbuyNum);

        bdNewbuyAmount = new BigDecimal(newbuyAmount);
    }


    private void calculate() {
        initView();

        if (bdOldAmount == null
                || bdOldCurAmount == null
                || bdNum == null
                || bdNewbyNum == null
                || bdNewbuyAmount == null) {
            Toast.makeText(MainActivity.this, "数据不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        tvOldTotal.setText(bdOldCurAmount.multiply(bdNum)  + "");


        BigDecimal bdCut = bdOldCurAmount.subtract(bdOldAmount);
        tvOldGet.setText(bdCut.multiply(bdNum) + "");

        Toast.makeText(MainActivity.this, "" + bdCut.toString(), Toast.LENGTH_SHORT).show();
        if (bdCut.compareTo(BigDecimal.ZERO) == -1) {
            BigDecimal bdOldPercent = bdCut.divide(bdOldAmount, 5, BigDecimal.ROUND_HALF_EVEN);

//            Log.v("stock", bdOldPercent.abs().multiply(new BigDecimal(100)) + "");
//            Log.v("stock", bdNewbyNum.divide(bdNum.add(bdNewbyNum), 5, BigDecimal.ROUND_HALF_EVEN) + "");
//            Log.v("stock", bdOldPercent.abs().multiply(bdNewbyNum.divide(bdNum.add(bdNewbyNum), 5, BigDecimal.ROUND_HALF_EVEN)) + "");
//            Log.v("stock", bdOldPercent.add(bdOldPercent.abs().multiply(bdNewbyNum.divide(bdNum.add(bdNewbyNum), 5, BigDecimal.ROUND_HALF_EVEN)))+ "");

            tvOldPercent.setText((bdOldPercent).multiply(new BigDecimal(100)) + "%");
            tvNewPercent.setText((bdOldPercent.add(bdOldPercent.abs().multiply(bdNewbyNum.divide(bdNum.add(bdNewbyNum), 5, BigDecimal.ROUND_HALF_EVEN)))).multiply(new BigDecimal(100)) + "%");

            tvNewAmount.setText((bdOldAmount.multiply(bdNum).add(bdNewbuyAmount.multiply(bdNewbyNum)).divide(bdNum.add(bdNewbyNum), 5, BigDecimal.ROUND_HALF_EVEN)) + "");

        } else {

            BigDecimal bdOldPercent = bdCut.divide(bdOldAmount, 5, BigDecimal.ROUND_HALF_EVEN);

            tvOldPercent.setText((bdOldPercent).multiply(new BigDecimal(100)) + "%");
            tvNewPercent.setText((bdOldPercent.subtract(bdOldPercent.multiply(bdNewbyNum.divide(bdNum.add(bdNewbyNum), 5, BigDecimal.ROUND_HALF_EVEN)))).multiply(new BigDecimal(100)) + "%");

            tvNewAmount.setText((bdOldAmount.multiply(bdNum).add(bdNewbuyAmount.multiply(bdNewbyNum)).divide(bdNum.add(bdNewbyNum), 5, BigDecimal.ROUND_HALF_EVEN)) + "");
        }

        //tvOldPercent.setText(bdCut.divide(bdOldAmount) + "%");


        tvNewCurAmount.setText(bdOldCurAmount + "/" + bdNewbuyAmount);
        tvNewNum.setText(bdNum.add(bdNewbyNum) + "");
        tvNewTotal.setText(bdOldCurAmount.multiply(bdNum).add(bdNewbuyAmount.multiply(bdNewbyNum)) + "");

//
//        if (bdCut.compareTo(BigDecimal.ZERO) == -1) {
//            Log.v("stock", bdCut.abs() + "");
//            Log.v("stock", bdNewbyNum.divide(bdNum.add(bdNewbyNum), 5, BigDecimal.ROUND_HALF_EVEN) + "");
//            Log.v("stock", bdCut.abs().multiply(bdNewbyNum.divide(bdNum.add(bdNewbyNum), 3)) + "");
//
//            //Log.v("stock", bdNewbuyAmount.divide(bdNum.add(bdNewbyNum), 3)));
//
//            showToast(bdCut.abs() + "");
//
//
//        } else {
//
//        }

    }

    private void showToast(String s) {
        Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
    }
}
